package com.block.channelext.service.channel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channelext.model.result.ReportResult;
import com.block.channelext.service.IChannelHandleService;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SuoZhiFlowChannelHandleService implements IChannelHandleService {

    private String requestUrl = "https://api.suozhikeji.cn/api/reportEvent";
    private String appkey = "ad_key_100400";
    private String code = "ad_code_90001395";

    private static Logger log = LogFactory.getCommLog();

    @Override
    public ReportResult registerAction(ChannelInfo channelInfo, JSONObject extJson) {

        String szRli = extJson.getString("szRli");
        if (StrUtil.isEmpty(szRli)){
            return ReportResult.newInstance();
        }

        String time = String.valueOf(System.currentTimeMillis());
        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("appkey", appkey);
        bizMap.put("logId", szRli);
        bizMap.put("time", time);
        bizMap.put("eventType", "register");

        String sign = this.getSign(bizMap);
        bizMap.put("sign", sign);

        log.info("【索知信息流上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(bizMap))
                .execute().body();
        log.info("【索知信息流上报】【注册】【开始结束】【result】{}", response);

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), response);
        reportResult.setSuccess(true);
        reportResult.setCode("0");
        reportResult.setMessage("未知返回值");
        return reportResult;

        }

    @Override
    public ReportResult authAction(ChannelInfo channelInfo, JSONObject extJson) {

        if (true)return ReportResult.newInstance();

        String szRli = extJson.getString("szRli");
        if (StrUtil.isEmpty(szRli)){
            return ReportResult.newInstance();
        }

        String time = String.valueOf(System.currentTimeMillis());
        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("appkey", appkey);
        bizMap.put("logId", szRli);
        bizMap.put("time", time);
        bizMap.put("eventType", "download");

        String sign = this.getSign(bizMap);
        bizMap.put("sign", sign);

        log.info("【索知信息流上报】【认证】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(bizMap))
                .execute().body();
        log.info("【索知信息流上报】【认证】【开始结束】【result】{}", response);

        return ReportResult.newInstance();
    }

    @Override
    public ReportResult proUvAction(ChannelInfo channelInfo, JSONObject extJson) {

        if (true)return ReportResult.newInstance();

        String szRli = extJson.getString("szRli");
        if (StrUtil.isEmpty(szRli)){
            return ReportResult.newInstance();
        }

        String time = String.valueOf(System.currentTimeMillis());
        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("appkey", appkey);
        bizMap.put("logId", szRli);
        bizMap.put("time", time);
        bizMap.put("eventType", "payment");

        String sign = this.getSign(bizMap);
        bizMap.put("sign", sign);

        log.info("【索知信息流上报】【产品点击】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(bizMap))
                .execute().body();
        log.info("【索知信息流上报】【产品点击】【开始结束】【result】{}", response);

        return ReportResult.newInstance();
    }

    public String getSign(Map<String, Object> bizMap){

        List<String> keyList = JSON.parseArray(JSON.toJSONString(bizMap.keySet()), String.class);
        Collections.sort(keyList);
        StringBuilder stringBuilder = new StringBuilder();
        for (String key: keyList){
            stringBuilder.append(StrUtil.format("{}={}", key, bizMap.get(key))).append("&");
        }
        stringBuilder.append(StrUtil.format("code={}", code));
        System.out.println(stringBuilder.toString());
        return SecureUtil.md5(stringBuilder.toString()).toLowerCase();

    }
}
